﻿using HDS.Data;
using HDS.IService.Master;
using HDS.Model;
using HDS.Model.Entity.Master;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace HDS.Service.Master
{
    public class HotelService : IHotelService
    {
        private MasterDbContext db = new MasterDbContext();

        public async Task<Hotel> GetHotel(int hotelId)
        {
            return await db.Hotel.GetSingleAsync(e => e.HotelId.Equals(hotelId));
        }

        public async Task<DataPagingModel<Hotel>> GetHotelList(int pageIndex, int pageSize, Expression<Func<Hotel, bool>> whereExpression, string whereSql)
        {
            RefAsync<int> total = 0;
            var rows = await db.Db.Queryable<Hotel>()
                .WhereIF(whereExpression != null, whereExpression)
                .WhereIF(!string.IsNullOrEmpty(whereSql), whereSql)
                .OrderBy(e => e.HotelId, OrderByType.Desc)
                .ToPageListAsync(pageIndex, pageSize, total);

            return new DataPagingModel<Hotel>
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                PageTotal = (total.Value + pageSize - 1) / pageSize,
                Total = total.Value,
                Rows = rows
            };
        }

        public async Task<int> UpdateHotel(Hotel entity)
        {
            if (!string.IsNullOrEmpty(entity.CountryCode))
            {
                var country = await db.Db.Queryable<Country>().Where(e => e.CountryCode.Equals(entity.CountryCode)).FirstAsync();
                if (country != null)
                    entity.CountryName = country.CountryNameEn;
            }

            if (!string.IsNullOrEmpty(entity.CityCode))
            {
                var city = await db.Db.Queryable<City>().Where(e => e.CityCode.Equals(entity.CityCode)).FirstAsync();
                if (city != null)
                    entity.CityName = city.CityNameEn;
            }

            entity.HotelMd5 = Hotel.CreateHotelMd5(entity);

            if (entity.HotelId <= 0)
            {
                return await db.Hotel.InsertReturnIdentityAsync(entity);
            }
            else
            {
                await db.Db.Updateable(entity).ExecuteCommandAsync();
                return entity.HotelId;
            }
        }

        public async Task DeleteHotel(int hotelId)
        {
            await db.Hotel.UpdateAsync(e => new Hotel { HotelStatus = false }, e => e.HotelId == hotelId);
            await db.HotelRoom.UpdateAsync(e => new HotelRoom { RoomStatus = false }, e => e.HotelId == hotelId);
            await db.Db.Deleteable<MappingHotel>().Where(e => e.HotelId.Equals(hotelId)).ExecuteCommandAsync();
            await db.Db.Deleteable<MappingRoom>().Where(e => e.HotelId.Equals(hotelId)).ExecuteCommandAsync();
        }


        public async Task<HotelRoom> GetRoom(int roomId)
        {
            return await db.HotelRoom.GetSingleAsync(e => e.RoomId.Equals(roomId));
        }

        public async Task<List<HotelRoom>> GetRoomList(int hotelId)
        {
            return await db.HotelRoom.GetListAsync(e => e.HotelId.Equals(hotelId));
        }

        public async Task<int> UpdateRoom(HotelRoom entity)
        {
            entity.RoomMd5 = HotelRoom.CreateRoomMd5(entity);
            if (entity.RoomId <= 0)
            {
                return await db.HotelRoom.InsertReturnIdentityAsync(entity);
            }
            else
            {
                await db.Db.Updateable(entity).ExecuteCommandAsync();
                return entity.RoomId;
            }
        }

        public async Task DeleteRoom(int roomId)
        {
            await db.HotelRoom.UpdateAsync(e => new HotelRoom { RoomStatus = false }, e => e.RoomId == roomId);
            await db.Db.Deleteable<MappingRoom>().Where(e => e.RoomId.Equals(roomId)).ExecuteCommandAsync();
        }

        public async Task RestoreRoom(int roomId)
        {
            await db.HotelRoom.UpdateAsync(e => new HotelRoom { RoomStatus = true }, e => e.RoomId == roomId);
        }
    }
}
